#include "myserialport.h"
#include <QDebug>
//#include <TSettings>
#include "common/tsettings/tsettings.h"
#include "common/common.h"
#include "settings/parametersettings.h"
MySerialport* MySerialport::mSp1 = NULL;
MySerialport* MySerialport::mSp2 = NULL;
QStringList MySerialport::mBaudList = QStringList()<<"1200"<<"2400"<<"4800"<<"9600"<<"19200"<<"38400"<<"57600"<<"115200";
QStringList MySerialport::mDataBitsList = QStringList()<<"8"<<"7";
QStringList MySerialport::mParityList = QStringList()<<"No"<<"Odd"<<"Even";
QStringList MySerialport::mFlowCtrlList = QStringList()<<"No";
QStringList MySerialport::mStopList = QStringList()<<"1"<<"2";

MySerialport::MySerialport(const QString& name,QObject *parent) : QObject(parent)
{
    mCom = new QSerialPort(name,parent);
    if(!mCom->open(QIODevice::ReadWrite))
    {
        qDebug()<<mCom->error();
        return;
    }qDebug()<<__LINE__;

    if(QString::compare(name,"ttyS1")==0)
    {
        mSettingGroupName = ParameterSettings::COM1_GROUP;
    }
    if(QString::compare(name,"ttyS2")==0)
    {
        mSettingGroupName = ParameterSettings::COM2_GROUP;
    }qDebug()<<__LINE__;
    TSettings set(DIR_SETTINGS,TSettings::IniFormat);qDebug()<<__LINE__;
    set.beginGroup(mSettingGroupName);
    int baud = set.value(ParameterSettings::BAUDRATE,QSerialPort::Baud9600).toInt();
    int databits = set.value(ParameterSettings::DATABITS,QSerialPort::Data8).toInt();
    int parity = set.value(ParameterSettings::PARITY,QSerialPort::NoParity).toInt();
    int fctrl = set.value(ParameterSettings::FLOWCTRL,QSerialPort::NoFlowControl).toInt();
    int stopbits = set.value(ParameterSettings::STOPBITS,QSerialPort::OneStop).toInt();
    set.endGroup();
    initComSetting(baud,databits,fctrl,parity,stopbits);
    connect(mCom,SIGNAL(readyRead()),this,SLOT(onRead()));qDebug()<<__LINE__;
}

void MySerialport::__setBraudRate(int baudRate)
{
    mCom->setBaudRate(baudRate);
}

void MySerialport::__setDataBits(int dataBits)
{
    mCom->setDataBits((QSerialPort::DataBits)dataBits);
}

void MySerialport::__setStopBits(int stopBits)
{
    mCom->setStopBits((QSerialPort::StopBits)stopBits);
}

void MySerialport::__setFlowCtrl(int flowControl)
{
    mCom->setFlowControl((QSerialPort::FlowControl)flowControl);
}

void MySerialport::__setParity(int parity)
{
    mCom->setParity((QSerialPort::Parity)parity);
}


MySerialport *MySerialport::getObjCom1()
{
    if(mSp1==NULL)
    {
        mSp1 = new MySerialport("ttyS1");
    }
    return mSp1;
}

MySerialport *MySerialport::getObjCom2()
{
    if(mSp2==NULL)
    {
        mSp2 = new MySerialport("ttyS2");
    }
    return mSp2;
}

QString MySerialport::getBaudRateStr()
{
    qint32 baudrate = mCom->baudRate();
    switch (baudrate) {
    case QSerialPort::Baud1200:
        return mBaudList.at(0);
        break;
    case QSerialPort::Baud2400:
        return mBaudList.at(1);
        break;
    case QSerialPort::Baud4800:
        return mBaudList.at(2);
        break;
    case QSerialPort::Baud9600:
        return mBaudList.at(3);
        break;
    case QSerialPort::Baud19200:
        return mBaudList.at(4);
        break;
    case QSerialPort::Baud38400:
        return mBaudList.at(5);
        break;
    case QSerialPort::Baud57600:
        return mBaudList.at(6);
        break;
    case QSerialPort::Baud115200:
        return mBaudList.at(7);
        break;
    default:
        break;
    }
}

QString MySerialport::getDataBitsStr()
{
    QSerialPort::DataBits databits = mCom->dataBits();
    switch (databits) {
    case QSerialPort::Data8:
        return mDataBitsList.at(0);
        break;
    case QSerialPort::Data7:
        return mDataBitsList.at(1);
        break;
    default:
        break;
    }
}

QString MySerialport::getParityStr()
{
    QSerialPort::Parity parity = mCom->parity();
    switch (parity) {
    case QSerialPort::NoParity:
        return mParityList.at(0);
        break;
    case QSerialPort::OddParity:
        return mParityList.at(1);
        break;
    case QSerialPort::EvenParity:
        return mParityList.at(2);
        break;
    default:
        break;
    }
}

QString MySerialport::getFlowCtrlStr()
{
    QSerialPort::FlowControl flowc = mCom->flowControl();
    switch (flowc) {
    case QSerialPort::NoFlowControl:
        return mFlowCtrlList.at(0);
        break;
    default:
        break;
    }
}

QString MySerialport::getStopBitsStr()
{
    QSerialPort::StopBits sb = mCom->stopBits();
    switch (sb) {
    case QSerialPort::OneStop:
        return mStopList.at(0);
        break;
    case QSerialPort::TwoStop:
        return mStopList.at(1);
        break;
    default:
        break;
    }
}

QStringList MySerialport::getBaudRateList()
{
    return mBaudList;
}

QStringList MySerialport::getDataBitsList()
{
    return mDataBitsList;
}

QStringList MySerialport::getParityList()
{
    return mParityList;
}

QStringList MySerialport::getFlowCtrlList()
{
    return mFlowCtrlList;
}

QStringList MySerialport::getStopBitsList()
{
    return mStopList;
}

void MySerialport::initComSetting(int baud,int databits,int flow,int parity,int stopbits)
{
    __setBraudRate(baud);
    __setDataBits(databits);
    __setParity(parity);
    __setFlowCtrl(flow);
    __setStopBits(stopbits);
}

void MySerialport::onRead()
{

}
